package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin  //使用gateway代替NGINX 网关已经解决了跨域问题
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //登录接口
    @ApiOperation("登录")
    @PostMapping("/login")
    public R login(){
        //{"code":20000,"data":{"token":"admin-token"}}
        return R.ok().data("token","admin-token");
    }

    @ApiOperation("用户信息")
    @GetMapping("/info")
    public R info(){
        //{"code":20000,"data":{"roles":["admin"],
        // "introduction":"I am a super administrator",
        // "avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
        // "name":"Super Admin"}}
        Map<String,Object> map = new HashMap<>();
        map.put("roles","admin");
        map.put("introduction","I am a super administrator");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name","Super Admin");


        return R.ok().data(map);

    }


    //查询所有医院设置
    @ApiOperation(value = "查询所有医院设置列表")
    @GetMapping("/findAll")
    public R findAll(){
        try {
            int a=10/0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YyghException(20001,"自定义异常");
        }

        List<HospitalSet> hospitalSetList  = hospitalSetService.list();
        return  R.ok().data("list",hospitalSetList) ;
    }
    //逻辑删除功能实现
    @ApiOperation(value = "逻辑删除功能实现")
    @DeleteMapping("{id}")
    public R delHospSetById(@PathVariable Long id){
        boolean b = hospitalSetService.removeById(id);
        return R.ok();
    }
    //实现分页查询
    @ApiOperation("实现分页查询")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageList(@PathVariable Long page,
                      @PathVariable Long limit
                 ){

        Page<HospitalSet> hospitalSetPage = new Page<>(page, limit);
        hospitalSetService.page(hospitalSetPage);
        List<HospitalSet> records = hospitalSetPage.getRecords();
        long total = hospitalSetPage.getTotal();


        return R.ok().data("rows",records).data("total",total);


    }

    //带条件实现分页查询
    @ApiOperation("带条件实现分页查询")
    @PostMapping("/pageQuery/{page}/{limit}")
    public R pageQuery(@PathVariable Long page,
                      @PathVariable Long limit,
                       @RequestBody HospitalSetQueryVo hospitalSetQueryVo
    ){
    //取出参数
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        //验证空 拼写sql
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> hospitalSetPage = new Page<>(page, limit);
        hospitalSetService.page(hospitalSetPage,wrapper);
        List<HospitalSet> records = hospitalSetPage.getRecords();
        long total = hospitalSetPage.getTotal();
        return R.ok().data("rows",records).data("total",total);
    }

    //新增医院设置
    @ApiOperation("新增医院设置")
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet){
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //实现数据回显接口
    @ApiOperation("实现数据回显接口")
    @GetMapping("/getHospById/{id}")
    public R getHospById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item",hospitalSet);
    }
    //实现修改方法
    @ApiOperation("实现修改方法")
    @PostMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if(b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public R batchRemove(@RequestBody List<Long> ids){
        boolean remove = hospitalSetService.removeByIds(ids);
        if(remove){
            return R.ok();
        }else{
            return R.error();
        }
    }

    // 医院设置锁定和解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询记录
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //存入新状态
        hospitalSet.setStatus(status);
        //更新数据
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }








}
